package com.danicsoft.daide.serverclient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import com.danicsoft.daide.binary.Connection;
import com.danicsoft.daide.command.OrderSpec;
import com.danicsoft.daide.command.Turn;
import com.danicsoft.daide.command.UnitPos;
import com.danicsoft.daide.gui.GUITextbookFrame;
import com.danicsoft.daide.historian.ClientHistorian;
import com.danicsoft.daide.historian.ServerHistorian;
import com.danicsoft.daide.token.StandardToken;

/**
 * Constructs a new Observer object which can be used to monitor communication
 * and orders in a game.
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class Observer implements Player {

	/**
	 * Create a new Observer that observes the server at the given address and
	 * port.
	 * 
	 * @param address
	 *        The address of the server to connect to.
	 * @param port
	 *        The port of the server to connect to.
	 * @throws IOException
	 *         If we could not connect.
	 */
	public Observer(InetAddress address, int port) throws IOException {
		GUITextbookFrame tFrame = new GUITextbookFrame(0);
		ServerProxy myServer = new ServerProxy(address, port, new ServerHistorian(tFrame.getTextbook()));

		historian = new ClientHistorian(tFrame.getTextbook());
		myServer.observe(this);
	}

	
	/**
	 * Create a new observer that connects to localhost on the default port.
	 * @param args
	 */
	public static final void main(String[] args) {
		try {
			new Observer(InetAddress.getLocalHost(), Connection.DEFAULT_PORT);
		}
		catch (UnknownHostException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * The historian we will use for logging.
	 */
	private ClientHistorian historian;

	public void accept() {
		historian.logUpdateMessage(this, "We are accepted into the game.");

	}

	public void acceptOrder(OrderSpec order, byte note) {

	}

	public void adminMessage(String from, String message) {
		historian.logUpdateMessage(this, "Recieved admin message from " + from + ".\n" + message);

	}

	public String getName() {
		return "Observer";
	}

	public int getPasscode() {
		return 0;
	}

	public StandardToken getPower() {
		return null;
	}

	public PLAYER_STATE getState() {
		return PLAYER_STATE.OBSERVING;
	}

	public String getVersion() {
		return "1.0";
	}

	public void nextTurn(TurnInfo turn) {
		historian.logUpdateMessage(this, "Next turn has advanced: " + turn.turn);

	}

	public void notifyMapInfo(MapInfo mInfo) {
		historian.logUpdateMessage(this, "We are playing on map " + mInfo.name);

	}

	public void notifyOfMissingOrders(List<UnitPos> missingPositions) {

	}

	public void notifyOfMissingRemovals(int removalAmount) {

	}

	public void notifyOfMissingRetreats(List<UnitPos> missingRetreats) {

	}

	public void notifyUpcomingDeadline(int secondsLeft) {
		historian.logUpdateMessage(this, "Deadline in " + secondsLeft + " seconds.");

	}

	public void reject() {
		historian.logUpdateMessage(this, "We have been rejected from the game.");

	}

	public void startGame(StandardToken playerPower, int playerPasscode, VariantInfo info, TurnInfo startingInfo) {

	}

	public void startGame(TurnInfo startingTurn) {
		historian.logUpdateMessage(this, "The game is starting");

	}

	@Override
	public String toString() {
		return "Observer";
	}

	public void endGame(List<StandardToken> winners, Turn turn) {
		historian.logUpdateMessage(this, "Game has ended in " + turn);

	}

}
